package com.bbcare.followup.plat.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.AppException;
import com.bbcare.comm.HttpUtil;
import com.bbcare.comm.PropertiesUtils;
import com.bbcare.followup.plat.entity.DumpMessageRecord;
import com.bbcare.followup.plat.entity.DumpPlanUpload;
import com.bbcare.followup.plat.entity.model.CommonPushMessage;
import com.bbcare.followup.plat.entity.model.Message;
import com.bbcare.followup.plat.service.IDumpMessageRecordService;
import com.bbcare.followup.plat.store.ibatis.IDumpBreastRecordDao;
import com.bbcare.followup.plat.store.ibatis.IDumpMessageRecordDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 复旦儿科患者消息记录Service业务层处理
 *
 * @author gxl
 * @date 2020-11-03
 */
@Service
public class DumpMessageRecordServiceImpl   implements IDumpMessageRecordService
{
    @Autowired
    private IDumpMessageRecordDao iDumpMessageRecordDao;

    @Autowired
    private IDumpBreastRecordDao dumpBreastRecordMapper;
    /**
     * 查询复旦儿科患者消息记录
     *
     * @param id 复旦儿科患者消息记录ID
     * @return 复旦儿科患者消息记录
     */
    @Override
    public DumpMessageRecord selectDumpMessageRecordById(String id)
    {
        return iDumpMessageRecordDao.selectDumpMessageRecordById(id);
    }

    /**
     * 查询复旦儿科患者消息记录列表
     *
     * @param params 复旦儿科患者消息记录
     * @return 复旦儿科患者消息记录
     */
    @Override
    public List<DumpMessageRecord> selectDumpMessageRecordList(Map params)
    {
        return iDumpMessageRecordDao.selectDumpMessageRecordList(params);
    }

    /**
     * 新增复旦儿科患者消息记录
     *
     * @param DumpMessageRecord 复旦儿科患者消息记录
     * @return 结果
     */
    @Override
    public int insertDumpMessageRecord(DumpMessageRecord DumpMessageRecord)
    {
        DumpMessageRecord.setCreateTime(new Date());
        DumpMessageRecord.setId(UUID.randomUUID().toString());
        return iDumpMessageRecordDao.insertDumpMessageRecord(DumpMessageRecord);
    }

    /**
     * 修改复旦儿科患者消息记录
     *
     * @param DumpMessageRecord 复旦儿科患者消息记录
     * @return 结果
     */
    @Override
    public int updateDumpMessageRecord(DumpMessageRecord DumpMessageRecord)
    {
        return iDumpMessageRecordDao.updateDumpMessageRecord(DumpMessageRecord);
    }

    /**
     * 批量删除复旦儿科患者消息记录
     *
     * @param ids 需要删除的复旦儿科患者消息记录ID
     * @return 结果
     */
    @Override
    public int deleteDumpMessageRecordByIds(String[] ids)
    {
        return iDumpMessageRecordDao.deleteDumpMessageRecordByIds(ids);
    }

    /**
     * 删除复旦儿科患者消息记录信息
     *
     * @param id 复旦儿科患者消息记录ID
     * @return 结果
     */
    @Override
    public int deleteDumpMessageRecordById(String id)
    {
        return iDumpMessageRecordDao.deleteDumpMessageRecordById(id);
    }

    @Override
    public Integer selectCountDumpMessageRecord(Map params) {
        return iDumpMessageRecordDao.selectCountDumpMessageRecordList(params);
    }

    @Override
    @Transactional
    public void sendWxMessage(DumpPlanUpload dumpPlanUpload) {
        String posturl = PropertiesUtils.getProperty("dump_url");
        String linkur = PropertiesUtils.getProperty("dump_link_url");
        String wxTemplateId = PropertiesUtils.getProperty("dump_template_id");
        //获取需要发送人员的accountId
        Map params = new HashMap();
        params.put("dumpPlanId",dumpPlanUpload.getId());
        List<DumpMessageRecord> accountIds =  iDumpMessageRecordDao.selectDumpMessageRecordList(params);
        if(accountIds.size()==0){
            return;
        }
        List<String> ids  = new ArrayList<>();
        List<Map<String,String>> list = new ArrayList<>();
        for (DumpMessageRecord record: accountIds) {
            Map<String,String>  x = new HashMap<>();
            x.put("id",record.getPatientId());
            x.put("accountId",record.getAccountId());
            list.add(x);
            ids.add(record.getId());
        }

        CommonPushMessage commonPushMessage = new CommonPushMessage();
        Message message = new Message();
        message.setCategory(5);
        message.setDeptId("fddxekyyzzdept001");
        message.setTitle("母乳供应通知");
        message.setBody("您好,您有新的母乳供应通知");
        message.setIcon("../../image/09.png");
        message.setAlias("");
        message.setQueueId("12fddxekyyzzqueue001");
        message.setPtUsers(list);
        message.setLinkUrl(linkur);
        commonPushMessage.setMessage(message);
        commonPushMessage.setWxTemplateId(wxTemplateId);
        Map webParam = new HashMap();
        webParam.put("dumpPlanId",dumpPlanUpload.getId());
        commonPushMessage.setWebParam(webParam);
        Map wxTemplateParam = new HashMap();
        Map<String,String> first = new HashMap<>();
        first.put("value","母乳供应通知");
        wxTemplateParam.put("first",first);
        Map<String,String> keyword1 = new HashMap<>();
        keyword1.put("value",dumpPlanUpload.getAuth());
        wxTemplateParam.put("keyword1",keyword1);
        Map<String,String> keyword2 = new HashMap<>();
        keyword2.put("value","您好,您有新的母乳供应通知");
        wxTemplateParam.put("keyword2",keyword2);
        Map<String,String> remark = new HashMap<>();
        remark.put("value","点击可查看母乳供应量");
        wxTemplateParam.put("remark",remark);
        commonPushMessage.setWxTemplateParam(wxTemplateParam);
        String sendMessage = JSONObject.toJSONString(commonPushMessage);
        try {
            String finalPosturl = posturl;
            Thread thread=new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("===task start===");
                    try {
                        HttpUtil.postData(finalPosturl,sendMessage);
                        params.clear();
                        params.put("sendTime",new Date());
                        params.put("status",2);
                        params.put("ids",ids);
                        iDumpMessageRecordDao.updateRecordStatus(params);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println("===task finish===");
                }
            });

            thread.start();

        } catch (Exception e) {
            throw  new AppException("1000","通知发送失败");
        }
    }

    @Override
    public void saveDumpMessageRecord(DumpPlanUpload dumpPlanUpload) {
        List<DumpMessageRecord> list  = iDumpMessageRecordDao.getPatientList();
        for (DumpMessageRecord dumpMessageRecord :list) {
            dumpMessageRecord.setCreateTime(new Date());
            dumpMessageRecord.setStatus("1");
            dumpMessageRecord.setDumpPlanId(dumpPlanUpload.getId());
        }
        iDumpMessageRecordDao.insertBacth(list);
    }

    @Override
    public void receiptDumpMessageRecord(Map params) {
        iDumpMessageRecordDao.receiptDumpMessageRecord(params);
    }

    @Override
    public List<Map<String, Object>> getUnreadPatientId(String dumpPlanId) {
        List<Map<String,Object>> result = new ArrayList<>();
        Map<String, String> params = new HashMap<>();
        params.put("dumpPlanId",dumpPlanId);
        params.put("status","2");
        List<DumpMessageRecord> list = iDumpMessageRecordDao.selectDumpMessageRecordList(params);
        for (DumpMessageRecord record :list) {
            Map<String,Object>  baseInfo= new HashMap<>();
            List<Map<String,Object>> userInfo = dumpBreastRecordMapper.selectPatientBaseInfo(record.getPatientId());
            for (Map map:userInfo){
                baseInfo.put(map.get("question_id").toString(),map.get("question_answer"));
            }
            result.add(baseInfo);
        }
        return result;
    }


}
